<template>
    <div class="login-wrap">
        <div class="login-box">
            <div class="box-left">
                <div class="left-title">小博课</div>
                <div class="left-ts">
                    <span v-for="(item,index) in textList"
                          :key="index">
                        <span v-if="index == 1">{{ ' / ' }}</span>
                        <span class="margin-box"
                              :class="{'active': swiperIndex == index}">
                            {{ item }}
                        </span>
                        <span v-if="index == 1">{{ ' / ' }}</span>
                    </span>
                </div>
                <div class="swiper-container swiper-no-swiping">
                    <div class="swiper-wrapper">
                        <div class="swiper-slide">
                            <img class="swiper-img"
                                 src="../assets/login/dl_pic_zy@2x.png">
                        </div>
                        <div class="swiper-slide">
                            <img class="swiper-img"
                                 src="../assets/login/dl_pic_aq@2x.png">
                        </div>
                        <div class="swiper-slide">
                            <img class="swiper-img"
                                 src="../assets/login/dl_pic_gx@2x.png">
                        </div>
                    </div>
                </div>
            </div>

            <div class="box-right">
                <div class="right-title">密码登录</div>

                <div class="right-input">
                    <el-input v-model="param.userName"
                              :clearable="true"
                              placeholder="请输入账号"></el-input>

                </div>
                <div class="right-input">
                    <el-input placeholder="请输入密码"
                              v-model="param.password"
                              show-password></el-input>
                </div>
                <!-- <div class="bear">
                    <div class="bear-left">
                        <el-checkbox v-model="checked">记住密码</el-checkbox>
                    </div>
                    <div class="bear-right">忘记密码？</div>
                </div> -->
                <div class="btn"
                     @click="submitForm()">登录</div>
            </div>
        </div>
        <div class="logo"></div>
    </div>
</template>
<!-- 获取唯一标识 -->

<script>
import axios from '@/utils/axios.js';
import 'swiper/dist/css/swiper.css'
import Swiper from "swiper"

export default {
    components: {

    },
    data: function () {
        return {
            textList: [
                '专业', '安全', '高效'
            ],
            swiperIndex: 0,
            checked: true,
            campusId: 1,
            campus: [],
            param: {
                userName: '',
                password: '',
            },
            rules: {
                userName: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
                password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
            },
            clientId: "webApp",
            clientSecret: "webApp"
            // sayMsg
        };
    },
    created() {

    },
    activated() {
    },
    methods: {
        submitForm() {
            if (this.param.userName == '' || this.param.password == '') {
                this.$message.error('请输入账号和密码');
                return false;
            } else {
                let loading = this.$loading({ text: '登陆中...' })
                let data = {
                    userName: this.param.userName,
                    password: this.param.password,
                }
                axios({
                    url: "/admin/user/login",
                    method: "post",
                    data,
                }).then(result => {
                    if (result.code == 0) {
                        this.$message.success('登录成功', 'success');
                        sessionStorage.setItem('token', result.data);
                        if (this.checked == true) {
                            //传入账号名，密码，和保存天数3个参数
                            this.setCookie(this.param.userName, this.param.password, 7);
                        } else {
                            //清空Cookie
                            this.clearCookie();
                        }
                        console.log('>>>>>>>>>>>>>>>>>>>>>>>>>>>userInfo', sessionStorage.getItem('token'))
                        this.$router.push('/');
                        // this.getLoginType()

                    } else {
                        this.$message.error(result.errMsg || '登录失败');
                    }
                    loading.close()
                }).catch(err => {
                    console.log('>>>>>>>>>>>>>>>>>>>>>>>>>>>err', err)
                })
                loading.close()
            }
        },
        getLoginType() {
            axios({
                url: "/admin/user/info",
                method: "post",
            }).then(result => {
                let loginType = ''
                if (result.code == 0) {
                    if (result.data.type == 0 && result.data.schoolId == '0') {
                        loginType = 'root'
                    } else if (result.data.type == 0 && result.data.schoolId != '0') {
                        loginType = 'school'
                    }
                    sessionStorage.setItem('userInfo', JSON.stringify(result.data))
                    sessionStorage.setItem('loginType', loginType);
                    this.$router.push('/');
                } else {
                    this.$message.error(result.errMsg || '登录失败');
                }

            }).catch(err => {
                console.log('>>>>>>>>>>>>>>>>>>>>>>>>>>>err', err)
            })
        },
        //设置cookie
        setCookie(c_name, c_pwd, exdays) {
            var exdate = new Date(); //获取时间
            exdate.setTime(exdate.getTime() + 24 * 60 * 60 * 1000 * exdays); //保存的天数
            //字符串拼接cookie
            window.document.cookie = "userName" + "=" + c_name + ";path=/;expires=" + exdate.toGMTString();
            window.document.cookie = "password" + "=" + c_pwd + ";path=/;expires=" + exdate.toGMTString();
        },
        //读取cookie
        getCookie: function () {
            if (document.cookie.length > 0) {
                var arr = document.cookie.split('; '); //这里显示的格式需要切割一下自己可输出看下
                for (var i = 0; i < arr.length; i++) {
                    var arr2 = arr[i].split('='); //再次切割
                    //判断查找相对应的值
                    if (arr2[0] == 'userName') {
                        this.param.userName = arr2[1]; //保存到保存数据的地方
                    } else if (arr2[0] == 'password') {
                        this.param.password = arr2[1];
                    }
                }
            }
        },
        //清除cookie
        clearCookie: function () {
            this.setCookie("", "", -1); //修改2值都为空，天数为负1天就好了
        },

    },
    mounted() {
        let _this = this
        var mySwiper = new Swiper('.swiper-container', {
            autoplay: true,
            delay: 2000,
            loop: true, // 循环模式选项
            on: {
                slideChange: function () {
                    _this.swiperIndex = this.realIndex
                },
            },
        })
        var s = [];
        var hexDigits = '0123456789abcdef';
        for (var i = 0; i < 36; i++) {
            s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
        }
        s[14] = '4';
        s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1);
        s[8] = s[13] = s[18] = s[23] = '-';

        this.uuidA = s.join('');
        return this.uuidA;
    }

};
</script>

<style lang="scss" scoped>
.login-wrap {
    position: relative;
    width: 100%;
    height: 100%;
    opacity: 1;
    display: flex;
    justify-content: center;
    align-content: center;
    position: relative;
    .login-box {
        width: 100%;
        height: 100%;
        background-color: #fff;
        display: flex;
        justify-content: center;
        align-items: center;
        .box-left {
            width: 340px;
            height: 421px;
            .left-title {
                margin-bottom: 15px;
                height: 45px;
                font-size: 32px;
                font-family: PingFang SC;
                font-weight: bold;
                line-height: 40px;
                color: #333333;
                // letter-spacing: 220px;
                opacity: 1;
            }
            .left-ts {
                height: 28px;
                font-size: 20px;
                font-family: PingFang SC;
                font-weight: 400;
                line-height: 28px;
                color: #b3b3b3;
                opacity: 1;
                .margin-box {
                    margin: 0 15px;
                }
                span {
                    transition: all 0.3s;
                    &.active {
                        font-size: 24px;
                    }
                }
            }
            .swiper-container {
                width: 340px;
                height: 340px;
                .swiper-img {
                    width: 100%;
                    height: 100%;
                    background-size: 100%;
                }
            }
        }
        .box-right {
            width: 450px;
            height: 421px;
            background: #ffffff;
            box-shadow: 0px 0px 30px rgba(0, 0, 0, 0.08);
            opacity: 1;
            border-radius: 16px;
            display: flex;
            align-items: center;
            justify-content: center;
            flex-direction: column;
            margin-left: 236px;
            .right-title {
                height: 33px;
                font-size: 24px;
                font-family: PingFang HK;
                font-weight: 500;
                line-height: 33px;
                color: #333333;
                opacity: 1;
            }
            .right-input {
                width: 338px;
                height: 40px;

                opacity: 1;
                border-radius: 2px;
                margin-top: 34px;
            }
            .bear {
                width: 338px;
                height: 20px;
                display: flex;
                justify-content: space-between;
                align-items: center;
                margin-top: 34px;
                .bear-left {
                    height: 20px;
                }
                .bear-right {
                    height: 20px;
                    font-size: 14px;
                    font-family: PingFang SC;
                    font-weight: 400;
                    line-height: 20px;
                    color: #1b93f8;
                }
            }
            .btn {
                width: 338px;
                height: 48px;
                background: linear-gradient(91deg, #24befe 0%, #146cf1 100%);
                opacity: 1;
                border-radius: 44px;
                display: flex;
                justify-content: center;
                align-items: center;
                font-size: 16px;
                font-weight: 500;
                color: #ffffff;
                opacity: 1;
                margin-top: 34px;
            }
        }
    }
    .logo {
        width: 240px;
        height: 46px;
        position: absolute;
        top: 40px;
        left: 40px;
        background: url(../assets/login/logo@2x.png);
        background-size: 100%;
    }
}
.ms-title {
    width: 100%;
    line-height: 50px;
    text-align: center;
    font-size: 20px;
    color: #000;
    border-bottom: 1px solid #ddd;
}
.ms-login {
    position: absolute;
    left: 50%;
    top: 50%;
    width: 350px;
    margin: -190px 0 0 -175px;
    border-radius: 5px;
    background: rgba(255, 255, 255, 0.3);
    overflow: hidden;
}
.ms-content {
    padding: 30px 30px;
}
.login-btn {
    text-align: center;
}
.login-btn button {
    width: 100%;
    height: 36px;
    margin-bottom: 10px;
}
.login-tips {
    font-size: 12px;
    line-height: 30px;
    color: #fff;
}
</style>
<style >
.el-message {
    background: #ffffff !important;
    border: 2px solid #ffffff !important;
    box-shadow: 3px 3px 8px rgba(0, 0, 0, 0.16) !important;
    opacity: 1;
    border-radius: 4px !important;
}

.el-input__inner {
    line-height: 40px !important;
    height: 40px !important;
}
</style>